﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using ComicIsland.App.CIHelper;
using ComicIsland.App.Resources;
using ComicIsland.Common;
using ComicIsland.Common.Entities.LocalJson;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace ComicIsland.App.Views
{
    public partial class PersonalCenterAppConfigPage : PhoneApplicationPage
    {
        public PersonalCenterAppConfigPage()
        {
            InitializeComponent();
            //
            this.ToggleSwitchDefaultContent();
        }

        void ToggleSwitchDefaultContent()
        {
            //this.tgsGesture.Content = "右手";
            //this.tgsGesture.IsChecked = true;
            //this.tgsOrientation.Content = "竖屏";
            //this.tgsOrientation.IsChecked = true;

            //this.tgsShake.IsChecked = false;
            //this.tgsShake.Content = "关闭";



            this.tgsDownloadTip.Content = "开启";
            this.tgsDownloadTip.IsChecked = true;

            this.tgsUpdateTip.IsChecked = true;
            this.tgsUpdateTip.Content = "开启";

            this.tgsOnlyWIFI.IsChecked = true;
            this.tgsOnlyWIFI.Content = "开启";

            this.tgsClearCache.Content = "正在计算缓存大小...";
            this.tgsClearCache.IsChecked = false;
            Task.Run(() =>
            {
                if (!this.IsComputeCacheRunning)
                {
                    IsComputeCacheRunning = true;
                    this.BindCacheSize();
                }
                else
                {
                    this.tgsClearCache.Content = "正在计算缓存大小...";
                }
            });
            this.tgsClearCache.Click += tgsClearCache_Click;

            this.tgsPushToast.IsChecked = false;
            this.tgsPushToast.Content = "关闭";
            LocalAppConfigInfo appConfigInfo = new LocalAppConfigInfoHelper().Get();
            if (appConfigInfo != null && appConfigInfo.WpId > 0)
            {
                if (appConfigInfo.IsPush == (int)KEYS.PushToastSwitch.Open)
                {
                    this.tgsPushToast.IsChecked = true;
                    this.tgsPushToast.Content = "开启";
                }
            }
            this.tgsPushToast.Click += tgsPushToast_Click;
        }







        void SetToggleSwitchContent(ToggleSwitch tgs, string content)
        {
            tgs.Content = content;
        }

        //private void tgsGesture_Checked(object sender, RoutedEventArgs e)
        //{
        //    this.SetToggleSwitchContent(tgsGesture, "右手");
        //}

        //private void tgsGesture_Unchecked(object sender, RoutedEventArgs e)
        //{
        //    this.SetToggleSwitchContent(tgsGesture, "左手");
        //}

        //private void tgsOrientation_Checked(object sender, RoutedEventArgs e)
        //{
        //    this.SetToggleSwitchContent(this.tgsOrientation, "竖屏");
        //}

        //private void tgsOrientation_Unchecked(object sender, RoutedEventArgs e)
        //{
        //    this.SetToggleSwitchContent(tgsOrientation, "横屏");
        //}

        //private void tgsShake_Checked(object sender, RoutedEventArgs e)
        //{
        //    this.SetToggleSwitchContent(this.tgsShake, "开启");
        //}

        //private void tgsShake_Unchecked(object sender, RoutedEventArgs e)
        //{
        //    this.SetToggleSwitchContent(this.tgsShake, "关闭");
        //}

        private void tgsDownloadTip_Checked(object sender, RoutedEventArgs e)
        {
            this.SetToggleSwitchContent(this.tgsDownloadTip, "开启");
        }

        private void tgsDownloadTip_Unchecked(object sender, RoutedEventArgs e)
        {
            this.SetToggleSwitchContent(this.tgsDownloadTip, "关闭");
        }

        private void tgsUpdateTip_Unchecked(object sender, RoutedEventArgs e)
        {
            this.SetToggleSwitchContent(this.tgsUpdateTip, "关闭");
        }

        private void tgsOnlyWIFI_Unchecked(object sender, RoutedEventArgs e)
        {
            this.SetToggleSwitchContent(this.tgsOnlyWIFI, "关闭");
        }

        private void tgsUpdateTip_Checked(object sender, RoutedEventArgs e)
        {
            this.SetToggleSwitchContent(this.tgsUpdateTip, "开启");
        }

        private void tgsOnlyWIFI_Checked(object sender, RoutedEventArgs e)
        {
            this.SetToggleSwitchContent(this.tgsOnlyWIFI, "开启");
        }


        #region ******************************清查缓存******************************
        private bool IsComputeCacheRunning = false, IsClearCacheRunning = false;
        void tgsClearCache_Click(object sender, RoutedEventArgs e)
        {
            bool isChecked = Convert.ToBoolean(this.tgsClearCache.IsChecked);
            if (isChecked)
            {
                if (this.IsClearCacheRunning)
                {
                    this.SetToggleSwitchContent(this.tgsClearCache, "正在清理缓存数据...");
                }
                else
                {
                    if (MessageBoxResult.OK == MessageBox.Show("清除缓存内容包括（已下载漫画、应用缓存），账户数据不会清除，确认操作？", AppResources.ApplicationTitle, MessageBoxButton.OKCancel))
                    {
                        this.IsClearCacheRunning = true;
                        this.progressBar.Visibility = System.Windows.Visibility.Visible;
                        this.SetToggleSwitchContent(this.tgsClearCache, "正在清理缓存数据...");
                        Task.Run(() =>
                        {
                            this.CleanCacheSize();
                        });
                    }

                    else
                    {
                        this.tgsClearCache.IsChecked = true;
                    }
                }
            }
            else
            {
                this.progressBar.Visibility = System.Windows.Visibility.Visible;
                this.BindCacheSize();
            }
        }

        void BindCacheSize()
        {
            this.Dispatcher.BeginInvoke(() =>
            {
                this.tgsClearCache.Content = string.Format("当前应用共有 {0} MB缓存数据！", Math.Round(IsolatedStorageSettingHelper.GetCacheSize(), 2));
                this.progressBar.Visibility = System.Windows.Visibility.Collapsed;
                this.IsComputeCacheRunning = false;
            });
        }

        void CleanCacheSize()
        {
            this.Dispatcher.BeginInvoke(() =>
            {
                IsolatedStorageSettingHelper.RemoveIsolateStorageByAll();
                this.tgsClearCache.Content = "缓存清理完成！";
                this.progressBar.Visibility = System.Windows.Visibility.Collapsed;
                this.IsClearCacheRunning = false;
            });
        }

        private void tgsClearCache_Checked(object sender, RoutedEventArgs e)
        {
            //this.SetToggleSwitchContent(this.tgsClearCache, "正在清理...");
        }

        private void tgsClearCache_Unchecked(object sender, RoutedEventArgs e)
        {
            // this.SetToggleSwitchContent(this.tgsClearCache, "共有80MB缓存数据");
        }
        #endregion ******************************清查缓存******************************


        #region ******************************推送开关******************************
        void tgsPushToast_Click(object sender, RoutedEventArgs e)
        {
            bool isChecked = Convert.ToBoolean(this.tgsPushToast.IsChecked);
            if (isChecked)
            {
                this.SetToggleSwitchContent(this.tgsPushToast, "开启");
                PushToastSwitch(Common.KEYS.PushToastSwitch.Open);
            }
            else
            {
                if (MessageBoxResult.OK == MessageBox.Show("关闭推送通知将无法接受最新的漫画更新提示，确认操作？", AppResources.ApplicationTitle, MessageBoxButton.OKCancel))
                {
                    this.SetToggleSwitchContent(this.tgsPushToast, "关闭");
                    PushToastSwitch(Common.KEYS.PushToastSwitch.Close);
                }
                else
                {
                    this.tgsPushToast.IsChecked = true;
                }
            }
        }

        private void tgsPushToast_Unchecked(object sender, RoutedEventArgs e)
        {
            //if (MessageBoxResult.OK == MessageBox.Show("关闭推送通知将无法接受最新的漫画更新提示，确认操作？", AppResources.ApplicationTitle, MessageBoxButton.OKCancel))
            //{
            //    this.SetToggleSwitchContent(this.tgsPushToast, "关闭");
            //    PushToastSwitch(Common.KEYS.PushToastSwitch.Close);
            //}
            //else
            //{
            //    this.tgsPushToast.IsChecked = true;
            //}
        }

        private void tgsPushToast_Checked(object sender, RoutedEventArgs e)
        {
            //this.SetToggleSwitchContent(this.tgsPushToast, "开启");
            //PushToastSwitch(Common.KEYS.PushToastSwitch.Open);
        }

        void PushToastSwitch(ComicIsland.Common.KEYS.PushToastSwitch pushToastSwitch)
        {
            Task.Run(() =>
            {
                LocalAppConfigInfo appConfigInfo = new LocalAppConfigInfoHelper().Get();
                if (appConfigInfo != null && appConfigInfo.WpId > 0)
                {
                    appConfigInfo.IsPush = (int)pushToastSwitch;
                    WinPhoneDataUpdateHelper updateHelper = new WinPhoneDataUpdateHelper();
                    updateHelper.InvokeInterface(Common.KEYS.WpDataUpdateType.updateOptions, appConfigInfo);
                }
            });
        }
        #endregion ******************************推送开关******************************

    }
}